package com.vercon.moensmart.presenter;

import androidx.annotation.Nullable;

import com.vercon.common.presenter.common.AbsPresenter;
import com.vercon.common.presenter.common.MirrorResultView;
import com.vercon.moensmart.BuildConfig;
import com.vercon.moensmart.event.LoginEvent;
import com.vercon.moensmart.mode.request.DeviceCommandRequest;
import com.vercon.moensmart.mode.request.DeviceIdRequest;
import com.vercon.moensmart.mode.request.MoenTekenRequest;
import com.vercon.moensmart.mode.response.AllDevices;
import com.vercon.moensmart.mode.response.BaseResponse;
import com.vercon.moensmart.mode.response.FollowMember;
import com.vercon.moensmart.mode.response.HealthMeasure;
import com.vercon.moensmart.mode.response.Inspiration;
import com.vercon.moensmart.mode.response.MeasureTrendChart;
import com.vercon.moensmart.mode.response.MoenToken;
import com.vercon.moensmart.mode.response.UserInfo;
import com.vercon.moensmart.newtork.MoenApi;
import com.vercon.moensmart.newtork.MoenMainApi;
import com.vercon.moensmart.newtork.UserObserve;

import org.greenrobot.eventbus.EventBus;

import io.reactivex.disposables.Disposable;


public class MoenPresenter extends AbsPresenter {

    private final String tokenUrl = "https://apidevunc.moeniot.com/";


    /**
     * 获取摩恩Token
     *
     * @param resultView 接口回调
     */
    public void getMoenToken(String username, String password, MirrorResultView<MoenToken> resultView) {
        if (compositeDisposable == null){
            return;
        }
        MoenTekenRequest moenTekenRequest;
        if(BuildConfig.DEBUG){
            moenTekenRequest = new MoenTekenRequest(username,password,"6602488303c9deb460427cbf");
        }else {
            moenTekenRequest = new MoenTekenRequest(username,password,"");
        }
        MoenMainApi.getInstance().getApi(tokenUrl, MoenApi.class,false)
                .getMoenToken(moenTekenRequest)
                .compose(schedulersTransformer())
                .subscribe(new UserObserve<MoenToken>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(MoenToken tokenBean) {
                        resultView.onSuccess(tokenBean);
                    }

                    @Override
                    public void onNetError(Throwable e) {
                        resultView.onError(0, e.toString());
                    }
                });
    }

    /**
     * 用户数据
     */
    public void getUserInfo(MirrorResultView<BaseResponse<UserInfo>> resultView){
        if (compositeDisposable == null){
            return;
        }
        MoenMainApi.getInstance().getApi(MoenApi.class,true)
                .getUserInfo()
                .map(new ServerResponseFunc<UserInfo>())
                .compose(schedulersTransformer())
                .subscribe(new UserObserve<BaseResponse<UserInfo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseResponse<UserInfo> userInfo) {
                        resultView.onSuccess(userInfo);
                    }

                    @Override
                    public void onNetError(Throwable e) {
                        resultView.onError(0, e.toString());
                    }
                });
    }


    /**
     * 摩恩灵感
     */
    public void moenAdList(MirrorResultView<BaseResponse<Inspiration>> resultView){
        if (compositeDisposable == null){
            return;
        }
        MoenMainApi.getInstance().getApi(MoenApi.class,true)
                .getInterceptor()
                .compose(schedulersTransformer())
                .subscribe(new UserObserve<BaseResponse<Inspiration>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseResponse<Inspiration> inspiration) {
                        resultView.onSuccess(inspiration);
                    }

                    @Override
                    public void onNetError(Throwable e) {
                        resultView.onError(0, e.toString());
                    }
                });
    }

    public void getAllDevice(MirrorResultView<BaseResponse<AllDevices>> resultView){
        if (compositeDisposable == null){
            return;
        }
        MoenMainApi.getInstance().getApi(MoenApi.class,true)
                .getAllDevices()
                .map(new ServerResponseFunc<AllDevices>())
                .compose(schedulersTransformer())
                .subscribe(new UserObserve<BaseResponse<AllDevices>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseResponse<AllDevices> allDevice) {
                        resultView.onSuccess(allDevice);
                    }

                    @Override
                    public void onNetError(Throwable e) {
                        resultView.onError(0, e.toString());
                    }
                });
    }

    public void followMember(MirrorResultView<BaseResponse<FollowMember>> resultView){
        if (compositeDisposable == null){
            return;
        }
        MoenMainApi.getInstance().getApi(MoenApi.class,true)
                .getFollowMember()
                .map(new ServerResponseFunc<FollowMember>())
                .compose(schedulersTransformer())
                .subscribe(new UserObserve<BaseResponse<FollowMember>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseResponse<FollowMember> followMember) {
                        resultView.onSuccess(followMember);
                    }

                    @Override
                    public void onNetError(Throwable e) {
                        resultView.onError(0, e.toString());
                    }
                });
    }

    public void getHealthMeasure(String year_month, String follow_enduser_id, String data_type, String page_size, String next,
                                 MirrorResultView<BaseResponse<HealthMeasure>> resultView){

        MoenMainApi.getInstance().getApi(MoenApi.class,true)
                .getHealthMeasure(year_month,follow_enduser_id,data_type,page_size,next)
                .map(new ServerResponseFunc<HealthMeasure>())
                .compose(schedulersTransformer())
                .subscribe(new UserObserve<BaseResponse<HealthMeasure>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseResponse<HealthMeasure> healthMeasure) {
                        resultView.onSuccess(healthMeasure);
                    }

                    @Override
                    public void onNetError(Throwable e) {
                        resultView.onError(0, e.toString());
                    }
                });
    }

    public void getMeasureTrendChart(String day_week_month_year, String pick_date, String pick_date_range,String pick_month,
                                    String pick_year,String follow_enduser_id,MirrorResultView<BaseResponse<MeasureTrendChart>> resultView){
        MoenMainApi.getInstance().getApi(MoenApi.class,true)
                .getMeasureTrendChart(day_week_month_year,pick_date,pick_date_range,pick_month,pick_year,follow_enduser_id)
                .map(new ServerResponseFunc<MeasureTrendChart>())
                .compose(schedulersTransformer())
                .subscribe(new UserObserve<BaseResponse<MeasureTrendChart>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseResponse<MeasureTrendChart> measureTrendChart) {
                        resultView.onSuccess(measureTrendChart);
                    }

                    @Override
                    public void onNetError(Throwable e) {
                        resultView.onError(0, e.toString());
                    }
                });
    }




    public void getDeviceInfo(String device_id,String product_id,MirrorResultView<BaseResponse<Object>> resultView){
            MoenMainApi.getInstance().getApi(MoenApi.class,true)
                    .getDeviceInfo(new DeviceIdRequest(device_id,product_id))
                    .compose(schedulersTransformer())
                    .subscribe(new UserObserve<BaseResponse<Object>>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            compositeDisposable.add(d);
                        }

                        @Override
                        public void onNext(BaseResponse<Object> measureTrendChart) {
                            resultView.onSuccess(measureTrendChart);
                        }

                        @Override
                        public void onNetError(Throwable e) {
                            resultView.onError(0, e.toString());
                        }
                    });
    }

    public void sendDeviceCommand(DeviceCommandRequest command, MirrorResultView<BaseResponse<Object>> resultView){
            MoenMainApi.getInstance().getApi(MoenApi.class,true)
                    .sendCommand(command)
                    .compose(schedulersTransformer())
                    .subscribe(new UserObserve<BaseResponse<Object>>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            compositeDisposable.add(d);
                        }

                        @Override
                        public void onNext(BaseResponse<Object> measureTrendChart) {
                            resultView.onSuccess(measureTrendChart);
                        }

                        @Override
                        public void onNetError(Throwable e) {
                            resultView.onError(0, e.toString());
                        }
                    });
    }



    //拦截固定格式的公共数据类型Response<T>,判断里面的状态码
    private class ServerResponseFunc<T>  implements io.reactivex.functions.Function<BaseResponse<T>, BaseResponse<T>>{
            @Override
            public BaseResponse<T> apply(BaseResponse<T> response) throws Exception {
                if(response.code != 0){
                    if(response.code == 10001 || response.code == 10002){
                        EventBus.getDefault().post(new LoginEvent());
                    }
                    throw new ServerException(response.code,response.message);
                }
                return response;
            }
    }
    public class ServerException extends RuntimeException {
        private int code;
        private String message;
        public ServerException(int code,String message){
            this.code = code;
            this.message = message;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        @Nullable
        @Override
        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }

}
